package com.github.baichuan.application.scaffold.rest.controller;

import com.github.baichuan.application.scaffold.rest.domain.organization.Tenant;
import com.github.baichuan.application.scaffold.rest.domain.organization.TenantService;
import com.github.baichuan.application.scaffold.rest.dto.tenant.TenantForm;
import com.github.baichuan.application.scaffold.rest.dto.tenant.TenantTableView;
import com.github.baichuan.application.scaffold.rest.dto.tenant.TenantQueryCriteria;
import com.github.baichuan.application.scaffold.rest.jpa.organization.TenantSpecification;
import com.github.baichuan.mapping.BeanMapper;
import com.github.baichuan.web.dto.Pagination;
import com.github.baichuan.web.dto.PaginationCriteria;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.servlet.Restful;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "租户")
@RestController
@RequestMapping("/tenant")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    @Operation(summary = "查询")
    @PostMapping("/query")
    public ResponseEntity<Pagination<TenantTableView>> query(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                                             @RequestBody PaginationCriteria<TenantQueryCriteria> paginationCriteria){
        TenantSpecification specification = paginationCriteria.getCriteria().specification(application);
        Page<Tenant> page = tenantService.query(specification, paginationCriteria.pageable(TenantTableView.class));
        Pagination<TenantTableView> pagination = Pagination.of(page).map(TenantTableView.class);
        return ResponseEntity.ok(pagination);
    }

    @Operation(summary = "获取表单数据")
    @GetMapping("/{id}")
    public ResponseEntity<TenantForm> form(@PathVariable("id") String id){
        Tenant tenant = tenantService.find(id);
        TenantForm form = BeanMapper.map(tenant, TenantForm.class);
        return ResponseEntity.ok(form);
    }

    @Operation(summary = "添加")
    @PostMapping
    public ResponseEntity<Result> add(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                      @RequestBody @Validated TenantForm form){
        Result result = tenantService.add(application, form);
        return Restful.created(result);
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    public ResponseEntity<Result> edit(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                       @PathVariable("id") String id, @RequestBody @Validated TenantForm form){
        Result result = tenantService.edit(id, application, form);
        return Restful.response(result);
    }
}
